﻿/**
* Title:"项目";
*
* Description:
*        功能：
*
* Author:Tongwel
*
* Date:2018.6
*
* Modify:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DemoSpace
{
    //用字典存放view 的信息 以及包括字典处理
    public class View 
    {
        //存放view的名称(避免view被注册多次)
        private Dictionary<string, Mediator> name2Mediator;
        //消息与view的字典
        private Dictionary<string, Mediator> msg2Mediator;
        
        //实例化
        public View()
        {
            name2Mediator = new Dictionary<string, Mediator>();
            msg2Mediator = new Dictionary<string, Mediator>();
        }

        //注册view
        public void ResiterView(Mediator mediator)
        {
            //假如不包含这个view名称 
            if (!name2Mediator.ContainsKey(mediator.Name))
            {
                //把事件名称添加给字典里
                foreach (var item in mediator.MsgList)
                {
                    this.msg2Mediator.Add(item, mediator);
                }
            }
        }

        //TODO : 删除注册（未测试）
        public void RemoveView(Mediator mediator)
        {
            if (name2Mediator.ContainsKey(mediator.Name))
            {
                for (int i = 0; i < mediator.MsgList.Count; i++)
                {
                    //删除字典的的事件
                    this.msg2Mediator.Remove(mediator.MsgList[i]);
                }
                name2Mediator.Remove(mediator.Name);
            }
        }

        //执行
        public void Execute(INotifier notifier)
        {
            if (msg2Mediator.ContainsKey(notifier.msg))
            {
                Mediator mediator = msg2Mediator[notifier.msg];
                mediator.Execute(notifier);
            }
        }

    }
}